home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ase.com / TMATHC.DOC < prev    next >
Encoding:
Text File  |  1989-08-06  |  8.1 KB  |  236 lines

  1.  
  2. ┌──────────────────────────────────────────────────────────────────┐
  3. │                                                                  │
  4. │               TriSoft Arithmetic String Evaluator                │
  5. │                   Turboc C v1.0 implementation                   │
  6. │                                                                  │
  7. │                         by Eric Chang                            │
  8. │                      and Stephen Chadwick                        │
  9. │                                                                  │
  10. │            (C) Copyright TriSoft Technologies    1989            │
  11. └──────────────────────────────────────────────────────────────────┘
  12.  
  13.     This Turbo C library provides the programmer with a fast and
  14. versatile arithmetic string processor (namely, a procedure that converts
  15. a string expression to a value).  Calculators and calculation
  16. routines are made simple and powerful, employing the use of string
  17. formulae rather than RPN or other notations.  What this means is that
  18. classic arithmetic notation (like the notation used in most programming
  19. languages like C or PASCAL) is used- that is, you type
  20.  
  21.  sin(4^16log(5-cos(26)))   instead of 26 [ENTER] cos [ENTER] etc. etc.
  22.  
  23.   In the development of this library, much importance was placed on making
  24. the routines easy to implement, while retaining their speed and flexibility.
  25. To do this, the main routine was split into two parts- the converter and the
  26. evaluator.  The converter checks the syntax and prepares the string for the
  27. evaluator, while also doing small optimizations to speed up final processing.
  28. This allows repetitive calculations, such as graphing or computing a series
  29. to be faster because converting does not have to be done more than once.
  30. The evaluator can evaluate complicated expressions very fast (to the order of
  31. a millisecond) and is therefore ideal for computations requiring a large
  32. number of smaller calculations using the same expression (e.g 2*X+1), but
  33. with different values of X, (e.g. for X=0,1,2,3...100). This process can be
  34. summarized by the following example:
  35.  
  36. Suppose the user wanted to compute the summation of an arbitrary function
  37. from x=1, to 100. Then his program will consist of the following steps:
  38.  
  39.  1) Enter an arbitrary arithmetic expression (like "2*X+1")
  40.  
  41.  2) Check for errors and convert this expression to a form which can
  42.         be easily recognized and efficiently processed by the evaluator.
  43.         This step can be accomplished by using the converter.
  44.  
  45.  3) Evaluate (100 times for different values of x) the form produced in
  46.         step 2 by using the evaluator 100 times for different values of x.
  47.  
  48. ______________________________________________________________________
  49.  
  50.  Included in this math package are 4 main files:
  51.  
  52.         tmathc.obj
  53.         tmathc87.obj
  54.         tmathc.h
  55.         tmathc87.h
  56.  
  57.  and 2 documentation files
  58.  
  59.         tmath.doc
  60.         license.doc
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. Description of Subroutines:
  69. ____________________________________________________________________
  70. Name: convert
  71.  
  72. Usage: #include <tmathc.h>
  73.              int convert(ELEM dest[],char source[])
  74.              <ELEM> is a structure defined in tmathc.h
  75.              (make sure to link in tmathc.obj)
  76.  
  77. Prototype in: tmathc.h
  78.  
  79. Description: Checks for syntax errors in the string source[]. It
  80.                          also converts the string source[] to an array of
  81.                          ELEM. This array can then be processed easily in
  82.                          function <eval>. The maximum size of source[] is 255
  83.                          characters and the maximum size of dest[] is 255 records.
  84.                          The procedure supports the following operators and functions:
  85.                          (the operators are listed in order of precedence)
  86.  
  87.  
  88.                              _          A_B -> A times ten to the B power.
  89.                                                     (e.g. 2.997_8 -> 2.997E+08)
  90.                              ()         Parentheses
  91.                              ^          Exponentiation
  92.                              *,/        Multiplication, Division
  93.                              +,-        Addition, Subtraction
  94.  
  95.                              SIN(X)     Sine of X
  96.                              COS(X)     Cosine of X
  97.                              TAN(X)     Tangent of X
  98.                              ARCSIN(X)  Principal Arc sine of X
  99.                              ARCCOS(X)  Principal Arc cosine of X
  100.                              ARCTAN(X)  Principal Arc tangent of X
  101.                              LN(X)      Log base e of X
  102.                              LOG(X)     Log base 1O of X
  103.                              EXP(X)     e to the X
  104.                              SQRT(X)    Square root of X
  105.  
  106.                         Register variables A..Z are also supported. (e.g.
  107.                         A+B+C will be a valid expression)
  108.                         The procedure is not case-sensitive and ignores blank
  109.                         spaces.
  110.                              In some cases, a multiplication sign * will not be
  111.                         necessary. For example the expression:
  112.                             AB+(3.4)(4.5) will be perfectly valid because the
  113.                         procedure will automatically insert multiplication signs
  114.                         in the appropriate places. The above expression will
  115.                         be converted to: A*B+(3.4)*(4.5) (note: the conversion
  116.                         is actually not physically done to source[], but to
  117.                         dest[])
  118.                             This procedure will not change the contents of
  119.                         source[].
  120.  
  121. Return Value: The procedure returns an integer representing a
  122.                             syntax error in source[]. dest[] will not be assigned and
  123.                             cannot be used by the function eval if this procedure
  124.                             should encounter an error in the string. Here is a table
  125.                             of the integer value and the corresponding error:
  126.  
  127.                                 return value     Meaning
  128.  
  129.                                     0              NO ERROR
  130.                                     1              PARENTHESES DO NOT MATCH
  131.                                     2              UNKNOWN CHARACTER
  132.                                     3              PARENTHESES DO NOT ENCLOSE ARGUMENT
  133.                                     4              TOO MANY OPERATORS OR OPERANDS
  134.                                     5              UNFORMATTED MANTISSA SEQUENCE
  135.                                     6              UNFORMATTED EXPONENTIAL SEQUENCE
  136.  
  137. ________________________________________________________________________
  138. Name: eval
  139.  
  140. Usage: #include <tmathc.h>
  141.              float eval(ELEM list[],float varlist[],float mode);
  142.              (make sure to link in tmathc.obj)
  143.  
  144. Prototype in: tmathc.h
  145.  
  146. Description: This function takes the array produced by the function
  147.                          <convert> and gives the evaluated result. The values
  148.                          of the registers A..Z are stored in varlist[0]..varlist[25].
  149.                          The parameter <mode> tells the function which trig mode to
  150.                          use. If mode=1.00, then the function will use radians. If
  151.                          mode=57.29577951, then it will use degrees, and if
  152.                          mode=63.66197724, then it will use gradians. The
  153.                          constants RAD, DEG, and GRAD are defined in tmathc.h as
  154.                          these values respectively.
  155.  
  156. Return value: Returns a real number representing the result of
  157.                             the evaluated expression.
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. Sample Program #1:
  167.  
  168. #include <stdio.h>
  169. #include <tmathc.h>
  170. /* This program computes the summation of any arbitrary function */
  171. /* of the variable a. It summates from a=1 to 100                */
  172. main()
  173.  {
  174.     char expression[255]; /* string holding the function of the variable a */
  175.     ELEM data[255]; /* converted form of the original string */
  176.     float varlist[26]; /* contains the values of the registers */
  177.     float sum=0.0,result;
  178.     int errnum; /* syntax error code */
  179.     int loop;
  180.  
  181.     printf("enter a function of the variable a: ");
  182.     scanf("%s",expression);
  183.     errnum=convert(data,expression);   /* only needs to be called once */
  184.     if (errnum==0)
  185.      {
  186.         for (loop=1; loop<=100; loop++)
  187.          {
  188.             varlist[0]=loop; /* let the variable register a have the value of loop */
  189.             result=eval(data,varlist,DEG); /* fast procedure for repetitive */
  190.                                                                          /* calculations */
  191.             sum=sum+result;
  192.          }
  193.         printf("Sum=%g\n",sum);
  194.      }
  195.     else printf("SYNTAX ERROR \n");
  196.  }
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. Sample program #2:
  204.  
  205.  
  206. #include <tmathc.h>
  207. #include <stdio.h>
  208.  
  209. /* This program asks for an arbitrary expression and evaluates it */
  210.  
  211. main()
  212.  {
  213.   char expression[255]; /* string holding the function of the variable a */
  214.     ELEM data[255]; /* converted form of the original string */
  215.     float varlist[26]; /* contains the values of the registers */
  216.     float result;
  217.     int errnum; /* syntax error code */
  218.  
  219.     printf("enter an expression: ");
  220.     scanf("%s",expression);
  221.     errnum=convert(data,expression);
  222.     if (errnum==0)
  223.      {
  224.         result=eval(data,varlist,DEG);
  225.         printf("Result=%g\n",result);
  226.      }
  227.     else printf("SYNTAX ERROR \n");
  228.  }
  229.  
  230.  
  231.  
  232.  
  233.     See LICENCE.DOC for licensing information.
  234.  
  235.  
  236.